package no.ntnu.item.ttm4115.termproject.teacheragent.gui;

import java.util.Vector;


import com.thoughtworks.xstream.XStream;

import no.ntnu.item.ttm4115.termproject.SendTreasureToConfigAgent;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionOption;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionQuestion;
import no.ntnu.item.ttm4115.termproject.extrasagent.Treasures;
import no.tellu.common.actorframe.ActorSM;
import no.tellu.common.javaframe.ActorAddress;

/*
 * AddTreasure.java
 *
 * Created on 29. juli 2008, 09:19
 */



/**
 *
 * @author  bruker
 */
public class AddTreasure extends javax.swing.JPanel {
	private TeacherGUI gui;
	private ActorSM asm;
	private Treasures treasure;
	private Vector<SessionQuestion> questions;
    /** Creates new form AddTreasure */
    public AddTreasure(TeacherGUI gui,ActorSM asm) {
        gui.setStartPanel(this);
        this.gui = gui;
        this.asm = asm;    	
    	initComponents();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this.gui.getContentPane());
        this.gui.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        headLabel = new javax.swing.JLabel();
        treasureNameLabel = new javax.swing.JLabel();
        treasureNameTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        addedQuestions = new javax.swing.JList();
        addQuestionsLabel = new javax.swing.JLabel();
        createTreasureButton = new javax.swing.JButton();
        newQuestionLabel = new javax.swing.JLabel();
        newQuestionNameLabel = new javax.swing.JLabel();
        option1Label = new javax.swing.JLabel();
        option2Label = new javax.swing.JLabel();
        option3Label = new javax.swing.JLabel();
        option4Label = new javax.swing.JLabel();
        newQuestionNameTextField = new javax.swing.JTextField();
        option1TextField = new javax.swing.JTextField();
        option2TextField = new javax.swing.JTextField();
        option3TextField = new javax.swing.JTextField();
        option4TextField = new javax.swing.JTextField();
        option1CheckBox = new javax.swing.JCheckBox();
        option2CheckBox = new javax.swing.JCheckBox();
        option3CheckBox = new javax.swing.JCheckBox();
        option4CheckBox = new javax.swing.JCheckBox();
        addNewQuestionButton = new javax.swing.JButton();
        removeSelectedButton = new javax.swing.JButton();
        clueLabel = new javax.swing.JLabel();
        clueTextField = new javax.swing.JTextField();
        xCoordLabel = new javax.swing.JLabel();
        xCoordTextField = new javax.swing.JTextField();
        yCoordLabel = new javax.swing.JLabel();
        yCoordTextField = new javax.swing.JTextField();
        addedQuestionsModel = new javax.swing.DefaultListModel();
        questions = new Vector<SessionQuestion>();
        headLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        headLabel.setText("New Treasure");

        treasureNameLabel.setText("Treasure Name");

        jScrollPane1.setViewportView(addedQuestions);

        addQuestionsLabel.setText("Added Questions");

        createTreasureButton.setText("Create Treasure");
        createTreasureButton.setEnabled(false);
        createTreasureButton.addActionListener(new java.awt.event.ActionListener(){
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		createTreasureButtonActionPerformed(evt);
        	}
        });

        newQuestionLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        newQuestionLabel.setText("New Question");

        newQuestionNameLabel.setText("Name");

        option1Label.setText("Option1");

        option2Label.setText("Option2");

        option3Label.setText("Option3");

        option4Label.setText("Option4");
        
        clueLabel.setText("Clue");

        xCoordLabel.setText("X Coord");

        yCoordLabel.setText("Y Coord");
        addedQuestions.setModel(addedQuestionsModel);
        
        addNewQuestionButton.setText("Add New Question");
        addNewQuestionButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		addNewQuestionActionPerformed(evt);
        	}
        });
        removeSelectedButton.setText("Remove Selected");
        removeSelectedButton.addActionListener(new java.awt.event.ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt){
        		removeSelectedActionPerformed(evt);
        	}
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headLabel)
                .addContainerGap(460, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(createTreasureButton)
                .addContainerGap(431, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(treasureNameLabel, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(treasureNameTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addQuestionsLabel)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(removeSelectedButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(yCoordLabel)
                            .addComponent(xCoordLabel)
                            .addComponent(clueTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                            .addComponent(clueLabel)
                            .addComponent(xCoordTextField)
                            .addComponent(yCoordTextField))))
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newQuestionLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(newQuestionNameLabel)
                            .addComponent(option1Label)
                            .addComponent(option2Label)
                            .addComponent(option3Label)
                            .addComponent(option4Label))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(option4TextField)
                            .addComponent(option3TextField)
                            .addComponent(option2TextField)
                            .addComponent(option1TextField)
                            .addComponent(newQuestionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(option4CheckBox)
                            .addComponent(option3CheckBox)
                            .addComponent(option2CheckBox)
                            .addComponent(option1CheckBox)))
                    .addComponent(addNewQuestionButton))
                .addGap(70, 70, 70))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(headLabel)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(treasureNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(treasureNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addQuestionsLabel)
                            .addComponent(clueLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(removeSelectedButton)
                                .addGap(43, 43, 43)
                                .addComponent(createTreasureButton)
                                .addGap(27, 27, 27))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(clueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(xCoordLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(xCoordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(yCoordLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(yCoordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newQuestionLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(newQuestionNameLabel)
                            .addComponent(newQuestionNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option1Label)
                            .addComponent(option1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option1CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option2Label)
                            .addComponent(option2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option2CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option3Label)
                            .addComponent(option3TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option3CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(option4Label)
                            .addComponent(option4TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(option4CheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addNewQuestionButton)
                        .addContainerGap())))
        );
    }// </editor-fold>
    private void addNewQuestionActionPerformed(java.awt.event.ActionEvent evt){
    	SessionQuestion question = new SessionQuestion();
    	Vector<String> texts = new Vector<String>();
    	texts.add(newQuestionNameTextField.getText());
    	question.setQuestionText(texts);
    	Vector<SessionOption> sessionOptions = new Vector<SessionOption>();
    	SessionOption option1 = new SessionOption();
    	option1.setAnswerOption(option1TextField.getText());
    	System.out.println(option1TextField.getText());
    	sessionOptions.add(option1);
    	SessionOption option2 = new SessionOption();
    	option2.setAnswerOption(option2TextField.getText());
    	sessionOptions.add(option2);
    	SessionOption option3 = new SessionOption();
    	option3.setAnswerOption(option3TextField.getText());
    	sessionOptions.add(option3);
    	SessionOption option4 = new SessionOption();
    	option4.setAnswerOption(option4TextField.getText());
    	sessionOptions.add(option4);
    	if(getCorrectAnswerIndex() != 9){
    		question.setCorrectAnswerIndex(getCorrectAnswerIndex());
    		//TODO gi feil meld
    	}
    	question.setOptions(sessionOptions);
    	questions.add(question);
    	addedQuestionsModel.addElement(newQuestionNameTextField.getText());
    	createTreasureButton.setEnabled(true);
    	resetQuestionScheme();
    }
    private void resetQuestionScheme(){
    	newQuestionNameTextField.setText("");
    	option1CheckBox.setSelected(false);
    	option2CheckBox.setSelected(false);
    	option3CheckBox.setSelected(false);
    	option4CheckBox.setSelected(false);
    	option1TextField.setText("");
    	option2TextField.setText("");
    	option3TextField.setText("");
    	option4TextField.setText("");
    }
    private void resetTreasureScheme(){
    	treasureNameTextField.setText("");
    	addedQuestionsModel.removeAllElements();
    	addedQuestions.repaint();
    	clueTextField.setText("");
    	xCoordTextField.setText("");
    	yCoordTextField.setText("");
    	
    }
    private void removeSelectedActionPerformed(java.awt.event.ActionEvent evt) {
    	Object[] list = addedQuestions.getSelectedValues();
    	removeQuestions(list);
    	addedQuestions.repaint();
    }
    
    private void removeQuestions(Object[] list){
    	if(list != null){
    		for (int i = 0; i < list.length; i++) {
				for (int j = 0; j < addedQuestionsModel.size(); j++) {
					if((list[i].toString()).equals(addedQuestionsModel.elementAt(j).toString())){
						System.out.println(addedQuestionsModel.elementAt(i));
						addedQuestionsModel.remove(i);
					}
				}
			}
    	}
    }
   
    private void createTreasureButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	Treasures treasure = new Treasures();
    	treasure.setName(treasureNameTextField.getText());
    	treasure.setQuestions(questions);
    	Vector<String> clues = new Vector<String>();
    	clues.add(clueTextField.getText());
    	treasure.setClue(clues);
    	
    	treasure.setXCoordinate(Integer.parseInt(xCoordTextField.getText()));
    	treasure.setYCoordinate(Integer.parseInt(yCoordTextField.getText()));
    	XStream xstream = new XStream();
		xstream.setClassLoader(new Treasures().getClass().getClassLoader());
		xstream.alias("treasures", Treasures.class);
		xstream.alias("sessionoption", SessionOption.class);
		xstream.alias("sessionquestion",SessionQuestion.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		String xmlTreasure = xstream.toXML(treasure);
		asm.sendMessage(new SendTreasureToConfigAgent(xmlTreasure),new ActorAddress("/configurationAgent","ConfigurationAgent"));
		gui.getTreasures().add(treasure);
    	createTreasureButton.setEnabled(false);
    	resetTreasureScheme();
    }
    
    private int getCorrectAnswerIndex(){
    	if(option1CheckBox.isSelected()){
    		return 0;
    	}else if(option2CheckBox.isSelected()){
    		return 1;
    	}else if(option3CheckBox.isSelected()){
    		return 2;
    	}else if(option4CheckBox.isSelected()){
    		return 3;
    	}else{
    		return 9;
    	}
    }
    


    // Variables declaration - do not modify
    private javax.swing.JButton addNewQuestionButton;
    private javax.swing.JLabel addQuestionsLabel;
    private javax.swing.JList addedQuestions;
    private javax.swing.JButton createTreasureButton;
    private javax.swing.JLabel headLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel newQuestionLabel;
    private javax.swing.JLabel newQuestionNameLabel;
    private javax.swing.JTextField newQuestionNameTextField;
    private javax.swing.JCheckBox option1CheckBox;
    private javax.swing.JLabel option1Label;
    private javax.swing.JTextField option1TextField;
    private javax.swing.JCheckBox option2CheckBox;
    private javax.swing.JLabel option2Label;
    private javax.swing.JTextField option2TextField;
    private javax.swing.JCheckBox option3CheckBox;
    private javax.swing.JLabel option3Label;
    private javax.swing.JTextField option3TextField;
    private javax.swing.JCheckBox option4CheckBox;
    private javax.swing.JLabel option4Label;
    private javax.swing.JTextField option4TextField;
    private javax.swing.JButton removeSelectedButton;
    private javax.swing.JLabel treasureNameLabel;
    private javax.swing.JTextField treasureNameTextField;
    private javax.swing.JLabel clueLabel;
    private javax.swing.JTextField clueTextField;
    private javax.swing.JLabel xCoordLabel;
    private javax.swing.JTextField xCoordTextField;
    private javax.swing.JLabel yCoordLabel;
    private javax.swing.JTextField yCoordTextField;
    private javax.swing.DefaultListModel addedQuestionsModel;
    // End of variables declaration

}
